package com.geetour.columbus.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author ChenKai
 */
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String beanId) throws BeansException {
        return applicationContext.getBean(beanId);
    }

    public static Object getBean(Class<?> beanClass) throws BeansException {
        return applicationContext.getBean(beanClass);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<?> getSubClassByType(Class<?> clazz) {
        Map map = applicationContext.getBeansOfType(clazz);
        return new ArrayList(map.values());
    }

}

